package com.atguigu.spzx.cloud.user.controller;

import com.atguigu.spzx.cloud.model.entity.User;
import com.atguigu.spzx.cloud.user.properties.MysqlProperties;
import com.atguigu.spzx.cloud.user.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

// com.atguigu.spzx.cloud.user.controller
@RestController
@RequestMapping(value = "/api/user")
@RefreshScope //动态获取nacos配置中心的配置
public class UserController {

    @Autowired
    private UserService userService ;
    @Value("${server.port}")
    private Integer port;

    //注入nacos配置中心的配置使用
    @Value("${app.name}")
    private String appName;
    @Resource
    MysqlProperties mysqlProperties;

    @GetMapping("/test")
    public String test(){
        System.out.println("appName = " + appName);
        System.out.println(mysqlProperties.getUrl()+" , " +
                mysqlProperties.getUsername());
        return "SUCCESS";
    }



    @GetMapping(value = "/findUserByUserId/{userId}")
    public User findUserByUserId(@PathVariable(value = "userId") Long userId) {
        //希望当前接口被调用时传入token
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String token = request.getHeader("token");
        System.out.println("token: "+ token);


        try {
            Thread.sleep(60);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("port: "+port+" 被访问了");
        return userService.findUserByUserId(userId) ;
    }

}